package com.bing.services.impl;

import com.bing.dao.PostDao;
import com.bing.dao.ResourceDao;
import com.bing.entity.Resource;
import com.bing.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/11/27.
 */

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    public static final String ROOT_RESOURCE = "from Resource where pid=null order by mTime desc";
    @Autowired
    private ResourceDao resourceDao;

    @Override
    public List<Resource> getRoot() {
        return resourceDao.findByHQL(ROOT_RESOURCE, null);
    }

    @Override
    public void addOrUpdate(Resource resource) {
        if (resource.getPid() != null) {
            Resource byId = resourceDao.findById(resource.getPid());
            resource.setParent(byId);
        }
        resourceDao.save(resource);
    }
}
